<?php

/**
 * aplicaciones actions.
 *
 * @package    auip
 * @subpackage aplicaciones
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class aplicacionesActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //$this->aplicacion_becas = Doctrine_Core::getTable('AplicacionBeca')
    //  ->createQuery('a')
    //  ->execute();
    //  $this->getUser()->setAttribute('email', Mensajero::desecriptar($request->getParameter('email')));
      $this->aplicacion_becas = AplicacionBecaTable::instancia($this->getUser()->getAttribute('email'));
      
      if($request->hasParameter('id'))
      {
          $this->getUser()->setAttribute('id', $request->getParameter('id'));
          //$this->forward('aplicaciones', 'new');
          $this->redirect('aplicaciones/new');
      }
  }

  public function executeAccion(sfWebRequest $request) {

       $this->getResponse()->setContentType('application/json');
       $string = $request->getParameter('q');       
       $req = Doctrine::getTable('AplicacionBeca')->miMetodo($string);       
       asort($req);//ordenar el resultado si no se hace en la consulta
       return $this->renderText(html_entity_decode(json_encode($req),ENT_QUOTES,'utf-8'));    
  }
   
   
  public function executeShow(sfWebRequest $request)
  {
    $this->aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'), $request->getParameter('usuario_id')));
    $this->forward404Unless($this->aplicacion_beca);
  }

  public function executeNew(sfWebRequest $request)
  {      
        $this->aplicacion_beca = new AplicacionBeca();   
        $this->aplicacion_beca->setBecaId($this->getUser()->getAttribute('id'));
        $this->aplicacion_beca->setBeca(BecaTable::getBecaEncontrada2($this->getUser()->getAttribute('id')));
        $this->aplicacion_beca->setUsuarioId($this->getUser()->getAttribute('email'));
        $this->forward404Unless($this->aplicacion_beca); 
        $this->form = new AplicacionBecaForm($this->aplicacion_beca);
        $tipo_beca = BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getTipoBeca()->getId();        
        /*permite encontrar las entidades dependiendo de la beca*/
        //echo '--->'.BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getTipoEntidad()->getId();
        //$this->form['entidad_destino']->getWidget()->setOption('query', Doctrine_Query::create()->select('e.id')->from('Entidad e')->where('e.tipo_entidad = ?', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getTipoEntidad()->getId()));
        
        /*SI LA BECA ES DE TIPO UNO EN LA ENTIDAD ORIGEN VAN LAS ENTIDADES QUE CORRESPONDEN AL PAIS DEL SOLICITANTE*/
        /*MIENTRAS QUE EL ENTIDAD DESTINO APARECEN TODAS LA ENTIDADES*/
        /*tipo de beca 1 se refiere a AUIP beca de movilidad*/
        if ($tipo_beca == 1)
        {
            $codigo_pais_origen = 154;
            $this->form['entidad_origen']->getWidget()->setOption('query', Doctrine_Query::create()->select('e.id')->from('Entidad e')->where('e.pais_id = ?', $codigo_pais_origen));            
        }
        if ($tipo_beca == 2)
        {
            /*tipo de beca movilidad entre instituciones aun no se definen los parametros*/
        }
        /*tipo de beca de posgrado entidad origen es la misma entidad que la entidad de destino*/
        if ($tipo_beca == 3)
        {
            $this->form['entidad_origen']->getWidget()->setOption('query', Doctrine_Query::create()->select('e.id')->from('Entidad e')->where('e.id = ?', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getEntidadQueConvoca()));            
            $this->form['entidad_destino']->getWidget()->setOption('query', Doctrine_Query::create()->select('e.id')->from('Entidad e')->where('e.id = ?', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getEntidadQueConvoca()));            
        }
        $this->getUser()->setAttribute('nom_beca', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getNombre());        
        $this->getUser()->setAttribute('nom_uni', BecaTable::getBecaEncontrada($this->getUser()->getAttribute('id'))->getEntidad()->getNombre());
  }

  public function executeCreate(sfWebRequest $request)
  {
      $this->aplicacion_beca = new AplicacionBeca();   
      $this->aplicacion_beca->setBecaId($this->getUser()->getAttribute('id'));
      $this->aplicacion_beca->setBeca(BecaTable::getBecaEncontrada2($this->getUser()->getAttribute('id')));
      $this->aplicacion_beca->setUsuarioId($this->getUser()->getAttribute('email'));
      $this->forward404Unless($this->aplicacion_beca);      
      $this->forward404Unless($request->isMethod(sfRequest::POST));
      $this->form = new AplicacionBecaForm($this->aplicacion_beca);
    //$this->form = new AplicacionBecaForm();
    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'),
              $request->getParameter('usuario_id'))), sprintf('Object aplicacion_beca does not exist (%s).', $request->getParameter('beca_id'),
              $request->getParameter('usuario_id')));
    $this->form = new AplicacionBecaForm($aplicacion_beca);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'),
              $request->getParameter('usuario_id'))), sprintf('Object aplicacion_beca does not exist (%s).', $request->getParameter('beca_id'),
              $request->getParameter('usuario_id')));
    $this->form = new AplicacionBecaForm($aplicacion_beca);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($aplicacion_beca = Doctrine_Core::getTable('AplicacionBeca')->find(array($request->getParameter('beca_id'),
              $request->getParameter('usuario_id'))), sprintf('Object aplicacion_beca does not exist (%s).', $request->getParameter('beca_id'),
              $request->getParameter('usuario_id')));
    $aplicacion_beca->delete();

    $this->redirect('aplicaciones/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));    
    if ($form->isValid())
    {
            
      $this->aplicacion_beca = $form->save();      
      //$this->redirect('aplicaciones/edit?beca_id='.$aplicacion_beca->getBecaId().'&usuario_id='.$aplicacion_beca->getUsuarioId());
      $this->redirect('documentos/new');
    }
  }
}
